﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Caching;

namespace GiaoTrinhEPU.Repositories.Repositories
{
    public class PublisherRepository : BaseRepository, IPublisherRepository
    {
        public PublisherRepository() : base() { }
        public PublisherRepository(ICacheProvider cacheProvider) : base(cacheProvider) { }

        /// <summary>
        /// Clear cache of all banks
        /// </summary>
        public void ClearCache()
        {
            Cache.Invalidate("publishers");
        }

        /// <summary>
        /// Clear cache of the publisher
        /// </summary>
        /// <param name="key"></param>
        public void ClearCache(String key)
        {
            Cache.Invalidate("publisher-" + key);
        }
        /// <summary>
        /// Create a new bank
        /// </summary>
        /// <param name="item"></param>
        public void CreatePublisher(Publisher item)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    db.Publishers.InsertOnSubmit(item);
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }

        // <summary>
        /// Update Publisher
        /// </summary>
        /// <param name="item"></param>
        public void UpdatePublisher(Publisher item)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var publisher = db.Publishers.Single(a => a.Id.Equals(item.Id));
                    publisher.Id = item.Id;
                    publisher.Name = item.Name;
                    publisher.Description = item.Description;
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }
        /// <summary>
        /// Delete a Publisher
        /// </summary>
        /// <param name="id"></param>
        public void DeletePublisher(int id)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var publisher = db.Publishers.Single(a => a.Id.Equals(id));
                    db.Publishers.DeleteOnSubmit(publisher);
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }
        /// <summary>
        /// List all Publisher in database, order by Publisher's name
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Publisher> ListPublishers(bool cacheEnabled = true)
        {
            if (cacheEnabled == true)
            {
                // if it's not in the cache, we need to read it form the repository// First, check the cache
                IEnumerable<Publisher> publisherData = Cache.Get("publishers") as IEnumerable<Publisher>;

                // if it's not in the cache, we need to read it form the repository
                if (publisherData == null)
                {
                    //Get the repository data
                    try
                    {
                        using (var db = new ThuVienEPUDataContext())
                        {
                            publisherData = db.Publishers.OrderBy(a => a.Name).ToList();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Writer.Write(ex.Message, ex.Source);
                    }

                    if (publisherData.Any())
                    {
                        Cache.Set("publishers", publisherData, GiaoTrinhEPU.Caching.Properties.Settings.Default.CacheTime);
                    }
                }
                return publisherData;
            }
            else
            {
                //Get the repository data
                IEnumerable<Publisher> publisherData = null;

                try
                {
                    using (var db = new ThuVienEPUDataContext())
                    {
                        publisherData = db.Publishers.OrderBy(a => a.Name).ToList();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Writer.Write(ex.Message, ex.Source);
                }

                return publisherData;
            }
        }
        /// <summary>
        /// Get a Publisher if we know it's id
        /// </summary>
        /// <param name="id">id of the banner</param>
        /// <returns>
        /// return a banner
        /// </returns>
        public Publisher GetPublisherById(int id)
        {
            Cache.Invalidate("publisher-" + id);
            Publisher publisherData = Cache.Get("publisher-" + id) as Publisher;
            if (publisherData == null)
            {
                try
                {
                    using (var db = new ThuVienEPUDataContext())
                    {
                        publisherData = db.Publishers.Single(a => a.Id.Equals(id));
                    }
                }
                catch (Exception ex)
                {
                    Logger.Writer.Write(ex.Message, ex.Source);
                }

                if (publisherData != null)
                {
                    Cache.Set("publisher-" + id, publisherData, GiaoTrinhEPU.Caching.Properties.Settings.Default.CacheTime);
                }
            }
            return publisherData;
        }
        /// <summary>
        /// check name of the publisher already exist in database 
        /// </summary>
        /// <param name="name"></param>
        /// <returns>
        /// return true if exist, else false
        /// </returns>
        public bool HasNameExisted(string name)
        {
            bool isExisted = false;
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var list = (from a in db.Publishers
                                where a.Name.Equals(name)
                                select a.Id).ToList();
                    isExisted = list.Count > 0;
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
            return isExisted;
        }

        /// <summary>
        /// check duplication of the banner in case of updating the banner
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(int id, string name)
        {
            bool isDuplicated = false;
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var list = (from a in db.Publishers
                                where a.Name.Equals(name)
                                where !a.Id.Equals(id)
                                select a.Id).ToList();
                    isDuplicated = list.Count > 0;
                }
            }
            catch (Exception ex)
            {

                Logger.Writer.Write(ex.Message, ex.Source);
            }
            return isDuplicated;
        }

    }
}
